package com.pureut.scheduling.service.impl;

import com.pureut.common.security.utils.SecurityUtils;
import com.pureut.scheduling.domain.SchedulingWeight;
import com.pureut.scheduling.domain.SchedulingWeightInclude;
import com.pureut.scheduling.domain.dto.SchedulingPlanDto;
import com.pureut.scheduling.domain.vo.SchedulingWeightVo;
import com.pureut.scheduling.domain.vo.SchedulingWillVo;
import com.pureut.scheduling.mapper.SchedulingWillMapper;
import com.pureut.scheduling.service.ISchedulingWillService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Description 排产条件实现类
 * @Date 22-12-29
 * @Author hl
 **/
@Service
public class SchedulingWillServiceImpl implements ISchedulingWillService {

    @Resource
    private SchedulingWillMapper schedulingWillMapper;

    /**
     * 查询排程条件信息
     *
     * @param schedulingWillVo 排程条件接受类
     * @return list 排程条件集合
     */
    @Override
    public List<SchedulingPlanDto> selectWillList(SchedulingWillVo schedulingWillVo) {
        return schedulingWillMapper.selectWillList(schedulingWillVo);
    }

    /**
     * 修改排序或开启关闭
     *
     * @param schedulingWillVo 排程条件接受类
     * @return list 排程条件集合
     */
    @Override
    public int updateSchedulingWillSort(SchedulingWillVo schedulingWillVo) {
        int px = schedulingWillVo.getWillType();
        int lp=0;
        int ups = 0;
        if(px!=0){
            if(px==1){
                //上
                lp =schedulingWillMapper.updateSchedulingSortOn(schedulingWillVo);
                if(lp>0) {
                    schedulingWillVo.setWillSort(schedulingWillVo.getWillSort() - 1);
                }
            }else{
                lp =schedulingWillMapper.updateSchedulingSortUnderThe(schedulingWillVo);
                if(lp>0){
                    schedulingWillVo.setWillSort(schedulingWillVo.getWillSort()+1);
                }
            }
        }
        ups = schedulingWillMapper.updateSchedulingWillSort(schedulingWillVo);
        return ups;
    }

    /**
     * 查询配比信息
     * @param schedulingWeightVo
     * @return
     */
    @Override
    public List<SchedulingWeight> selectWeightList(SchedulingWeightVo schedulingWeightVo) {
        return schedulingWillMapper.selectWeightList(schedulingWeightVo);
    }

    /**
     * 根据配比id查询详情
     * @param weightId
     * @return
     */
    @Override
    public SchedulingWeight selectWeightById(long weightId) {
        SchedulingWeight wei = schedulingWillMapper.selectWeightById(weightId);
        List<SchedulingWeightInclude> list = schedulingWillMapper.selectWeightLine(wei.getWeightId());
        wei.setWeightLineList(list);
        return wei;
    }

    /**
     * 新增配比信息
     * @param schedulingWeightVo
     * @return
     */
    @Override
    public int insertWeight(SchedulingWeightVo schedulingWeightVo) {
        schedulingWeightVo.setCreateBy(SecurityUtils.getLoginUser().getUsername());
        schedulingWeightVo.setCreateTime(new Date());
        int ji = schedulingWillMapper.insertWeight(schedulingWeightVo);
        if(ji>0){
            List<SchedulingWeightInclude> list = schedulingWeightVo.getWeightLineList();
            for (SchedulingWeightInclude include:
            list) {
                include.setWeightId(schedulingWeightVo.getWeightId());
                List<String> kk = include.getWeightLineType();
                StringBuilder str = new StringBuilder();
                for (String y:
                kk) {
                    str.append(y).append(",");
                }
                String kls = str.toString();
                kls = kls.substring(0,kls.length()-1);
                include.setWeightLineTypeS(kls);
            }
            ji = schedulingWillMapper.insertWeightLine(list);
        }
        return ji;
    }

    /**
     * 修改配比信息
     * @param schedulingWeightVo
     * @return
     */
    @Override
    public int updateWeight(SchedulingWeightVo schedulingWeightVo) {
        schedulingWeightVo.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
        schedulingWeightVo.setUpdateTime(new Date());
        int kl = schedulingWillMapper.updateWeight(schedulingWeightVo);
        if(kl>0){
           kl = schedulingWillMapper.delSchedulingWill(schedulingWeightVo.getWeightId());
           if(kl>0){
               List<SchedulingWeightInclude> list = schedulingWeightVo.getWeightLineList();
               for (SchedulingWeightInclude include:
                       list) {
                   include.setWeightId(schedulingWeightVo.getWeightId());
                   List<String> kk = include.getWeightLineType();
                   StringBuilder str = new StringBuilder();
                   for (String y:
                           kk) {
                       str.append(y).append(",");
                   }
                   String kls = str.toString();
                   kls = kls.substring(0,kls.length()-1);
                   include.setWeightLineTypeS(kls);
               }
               kl = schedulingWillMapper.insertWeightLine(list);
           }
        }
        return kl;
    }

    /**
     * 批量删除配比信息
     * @param weightIds
     */
    @Override
    public int deleteWeightByIds(String weightIds) {
        String[] weightIdS = weightIds.split(",");
        int kl = schedulingWillMapper.deleteWeightByIds(weightIdS);
        if(kl>0){
            for (String isS :
            weightIdS) {
                schedulingWillMapper.delSchedulingWill(Long.parseLong(isS));
            }
        }
       return kl;
    }

}
